<?php

class Users_mgr extends CI_Model {

    function __Construct() {
        parent::__Construct();
    }

    function select_all() {
        $session_login = $this->session->userdata('user_login');
        $this->db->select('*');
        $this->db->from('user');
        $this->db->join('tbl_role', 'user.role_id=tbl_role.role_id');
        $this->db->where('email', $session_login);
		return $this->db->get();
    }

    function all_users($limit, $start) {

        //$this->session->userdata('user_login');
        $this->db->limit($limit, $start);
        $this->db->select('*');
        $this->db->from('user');
        $this->db->where('id > 1');
        $this->db->join('tbl_role', 'user.role_id=tbl_role.role_id');
        return $this->db->get();
    }

    function delete_users($data) {
        $this->db->delete('user', $data);
    }

    //edit function
    function select_edit_user() {
        $id = $this->uri->segment('4');
        $this->db->select('*');
        $this->db->from('user');
        $this->db->join('tbl_role', 'user.role_id=tbl_role.role_id');
        $this->db->where('id', $id);

        return $this->db->get();
    }

    //update user here
    function update_user() {
        $this->load->library("form_validation");
        $query_str = array(
            'firstName' => $this->input->post('fname'),
            'lastName' => $this->input->post('lname'),
            'user_name' => $this->input->post('username'),
            'password' => md5($this->input->post('pass')),
            'user_gender' => $this->input->post('gender'),
            'email' => $this->input->post('email'),
            'user_nationality' => $this->input->post('nation'),
            'user_add' => $this->input->post('address'),
            'role_id' => $this->input->post('role')
        );
        $id = $this->input->post('getid');
        $this->db->where('id', $id);
        return $this->db->update('user', $query_str);
    }

    function checkexist($username) {
        $qu = "SELECT user_name FROM user WHERE user_name = ? ";
        $result = $this->db->query($qu, $username);
        if ($result->num_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    function addmembers() {
        $this->load->library("form_validation");

        $query_str = array(
            'firstname' => $this->input->post('fname'),
            'lastname' => $this->input->post('lastname'),
            'user_name' => $this->input->post('username'),
            'password' => sha1($this->input->post('pass')),
            'user_gender' => $this->input->post('gender'),
            'email' => $this->input->post('email'),
            'user_nationality' => $this->input->post('nationality'),
            'user_add' => $this->input->post('address'),
            'role_id' => $this->input->post('role')
        );

        return $this->db->insert('user', $query_str);
    }

}